package com.njupt.simulation.utils;

import com.njupt.simulation.entity.AdjacentBand;
import com.njupt.simulation.entity.workFreqBand;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import static com.alibaba.druid.sql.ast.SQLPartitionValue.Operator.List;

public class CalculateAdjacentBandPower {
    /**
     *
     * @param maxpower  受扰对象的最大发射功率
     * @param adjacentBandshourao  受扰或者干扰对象的邻频边带配置链表数据
     * @param adjacentBandganrao  受扰或者干扰对象的邻频边带配置链表数据
     * @return
     */
    public static double calculateadjacentbandpower(double maxpower, List<AdjacentBand> adjacentBandshourao, List<AdjacentBand> adjacentBandganrao) {
        double pt = 0;
        for (AdjacentBand adjacentBand1 : adjacentBandshourao) {
            for (AdjacentBand adjacentBand2 : adjacentBandganrao) {
                double x1 = Math.max(adjacentBand1.getStartFrequency(), adjacentBand2.getStartFrequency());
                double x2 = Math.min(adjacentBand1.getEndFrequency(), adjacentBand2.getEndFrequency());
                double y = Math.min(maxpower - adjacentBand1.getPowerLevel(), maxpower - adjacentBand2.getPowerLevel());
                if( x2 <= x1){
                    continue;
                }
                pt += (x2 - x1) * y;
            }
        }
        return pt;
    }

    /**
     * 计算是否存在重叠带宽
     * @param workFreqBand  信号中心频率和信号带宽
     * @param f_min  干扰信号频率最小值
     * @param f_max  干扰信号频率最大值
     * @return  重叠带宽大小
     */
    public static double calculateoverlapband(workFreqBand workFreqBand, double f_min, double f_max) {
        //f1是受扰信号中心频率，B是受扰信号带宽，f_min是干扰信号频段下界，f_max是干扰信号频段上界
        double f1 = workFreqBand.getMiddleFreq();
        double  B = workFreqBand.getBand();

        double B_chongdie = 0;
        if (f_max <= f1 - B / 2 || f_min >= f1 + B / 2) {
            B_chongdie = 0;
        } else if (f_max > f1 - B / 2 && f_max <= f1 + B / 2 && f_min <= f1 - B / 2) {
            B_chongdie = f_max - (f1 - B / 2);
        } else if (f_max > f1 - B / 2 && f_max <= f1 + B / 2 && f_min > f1 - B / 2) {
            B_chongdie = f_max - f_min;
        } else if (f_max > f1 + B / 2 && f_min >= f1 - B / 2 && f_min <= f1 + B / 2) {
            B_chongdie = f1 + B / 2 - f_min;
        }
        return  B_chongdie ;
    }

    public static void main(String[] args) {

//        List<AdjacentBand> adjacentBandshourao = new ArrayList<AdjacentBand>();
//        List<AdjacentBand> adjacentBandganrao = new ArrayList<AdjacentBand>();
//        for (AdjacentBand adjacentBand1 : adjacentBandshourao){
//            for(AdjacentBand adjacentBand2 : adjacentBandganrao){
//                double pt = calculateadjacentbandpower(30,adjacentBand1,adjacentBand2);
//            }
//        }






    }

}
